#include <iostream>
#include <unistd.h>	//fork()
#include <sys/types.h>	//getuid(), getgid()
#include <sys/wait.h>	//wait()
#include <stdlib.h>	//exit()

using namespace std;

int main (void)
{
	int i = 1;
	pid_t child;
	int status; 
	if ( 0 == ( child=fork()) ) 
	{
		{
			cout << "This is the child process. PID = " << getpid() << " GID = " << getgid() << "." << endl;
			cout << "i = " << i << " then change it to 3 in child proc:" << endl;
			i = 3;
			cout << "i = " << i << endl;
			sleep(5);
			exit(7);
		}
	}
	else 
	{
		wait(&status);
		cout << "i = " << i << " in father proc." << endl;
		cout << "This is the parent process. PID = " << getpid() << " GID = " << getgid() << "."  << endl;
		cout << "status = " << WEXITSTATUS(status) << endl;
	}
	cout << "Here the PID is " << getpid() << "." << endl;
	return 0;
}



/*
void main(void) 
{ 
	pid_t child; 
	int status; 

	printf("This will demostrate how to get child status\n"); 
	if((child=fork())==-1) 
	{ 
		printf("Fork Error :%s\n",strerror(errno)); 
		exit(1); 
	} 
	else if(child==0) 
	{ 
		int i; 
		printf("I am the child:%ld\n",getpid()); 
		for(i=0;i<1000000;i++) sin(i); 
		i=5; 
		printf("I exit with %d\n",i); 
		exit(i); 
	} 
	while(((child=wait(&status))==-1)&(errno==EINTR)); 
	if(child==-1) 
		printf("Wait Error:%s\n",strerror(errno)); 
	else if(!status) 
		printf("Child %ld terminated normally return status is zero\n", 
				child); 
	else if(WIFEXITED(status)) 
		printf("Child %ld terminated normally return status is %d\n", 
				child,WEXITSTATUS(status)); 
	else if(WIFSIGNALED(status)) 
		printf("Child %ld terminated due to signal %d znot caught\n", 
				child,WTERMSIG(status));   
}
//*/

